﻿using FlyingEye.HttpServices;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Windows;

namespace FlyingEye.Views.DeviceAppService
{
    /// <summary>
    /// 响应结果
    /// </summary>
    public class DefaultResponse
    {
        public DefaultResponse()
        {
        }

        /// <summary>
        /// 任务是否执行成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 任务信息
        /// </summary>
        public string? Message { get; set; }

        public string? Value { get; set; }
    }

    /// <summary>
    /// DeviceHubWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceHubWindow : Window
    {
        private readonly HttpClientService _httpClientService;

        private readonly HttpClientServiceOptions _options;

        private readonly HttpUrlsOptions _httpUrlsOptions;

        private readonly ILogger<DeviceClientManagementWindow> _logger;

        public DeviceHubWindow(
            HttpClientService httpClientService,
            IOptions<HttpClientServiceOptions> options,
            IOptions<HttpUrlsOptions> httpUrlsOptions,
            ILogger<DeviceClientManagementWindow> logger)
        {
            InitializeComponent();
            _httpClientService = httpClientService;
            _options = options.Value;
            _httpUrlsOptions = httpUrlsOptions.Value;
            _logger = logger;
            _options.ClientId = "DeviceAppServiceApp";
        }

        /// <summary>
        /// 执行配置
        /// </summary>
        private async void ConfigureButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.DeviceResourceIdTextBox.Text))
                {
                    throw new InvalidOperationException("设备ID不能为空");
                }

                var result = await _httpClientService.PostAsync<DefaultResponse, Object>(_httpUrlsOptions.DeviceAppServiceBaseUrl
                + $"/api/device-app-service/v1.0/device-clients/{this.DeviceResourceIdTextBox.Text}/config",
                null!);

                if (result.IsSuccess)
                {
                    MessageBox.Show("执行配置成功！");
                }
                else
                {
                    MessageBox.Show("执行配置失败：" + result.Message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "执行配置失败");
                MessageBox.Show("执行配置失败：" + ex.Message);
            }
        }

        private async void Value_Read_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.DeviceResourceIdTextBox.Text))
                {
                    throw new InvalidOperationException("设备ID不能为空");
                }

                var result = await _httpClientService.PostAsync<DefaultResponse, Object>(_httpUrlsOptions.DeviceAppServiceBaseUrl
                + $"/api/device-app-service/v1.0/device-clients/{this.DeviceResourceIdTextBox.Text}/read-numeric-value",
                new
                {
                    DeviceName = this.Value_DeviceName.Text,
                    Address = this.Value_Address.Text,
                    DataType = this.Value_DataType.Text,
                });
              
                if (result.IsSuccess)
                {
                    MessageBox.Show($"读取数据成功: Value = {result.Value}");
                }
                else
                {
                    MessageBox.Show("读取数据失败：" + result.Message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "读取数据失败");
                MessageBox.Show("读取数据失败：" + ex.Message);
            }
        }

        private async void Value_Write_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.DeviceResourceIdTextBox.Text))
                {
                    throw new InvalidOperationException("设备ID不能为空");
                }

                var result = await _httpClientService.PostAsync<DefaultResponse, Object>(_httpUrlsOptions.DeviceAppServiceBaseUrl
                + $"/api/device-app-service/v1.0/device-clients/{this.DeviceResourceIdTextBox.Text}/write-value",
                new
                {
                    DeviceName = this.Write_DeviceName.Text,
                    Address = this.Write_Address.Text,
                    DataType = this.Write_DataType.Text,
                    Value = this.Write_Value.Text,
                });

                if (result.IsSuccess)
                {
                    MessageBox.Show("写入数据成功！");
                }
                else
                {
                    MessageBox.Show("写入数据失败：" + result.Message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入数据失败");
                MessageBox.Show("写入数据失败：" + ex.Message);
            }
        }

        private async void String_Read_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(this.DeviceResourceIdTextBox.Text))
                {
                    throw new InvalidOperationException("设备ID不能为空");
                }

                var result = await _httpClientService.PostAsync<DefaultResponse, Object>(_httpUrlsOptions.DeviceAppServiceBaseUrl
                + $"/api/device-app-service/v1.0/device-clients/{this.DeviceResourceIdTextBox.Text}/read-string-value",
                new
                {
                    DeviceName = this.String_DeviceName.Text,
                    Address = this.String_Address.Text,
                    Length = this.String_Length.Text,
                });

                if (result.IsSuccess)
                {
                    MessageBox.Show($"读取数据成功: Value = {result.Value}");
                }
                else
                {
                    MessageBox.Show("读取数据失败：" + result.Message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "读取数据失败");
                MessageBox.Show("读取数据失败：" + ex.Message);
            }
        }
    }
}
